package com.breaktian.share.action;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.breaktian.share.Config;
import com.breaktian.share.Share;
import com.breaktian.share.ShareChannel;
import com.breaktian.share.ShareEntity;
import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.api.ImageObject;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.share.WbShareCallback;
import com.sina.weibo.sdk.share.WbShareHandler;
import com.tencent.connect.common.Constants;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.util.ArrayList;
/**
 * QQ
 * QZONE
 * WEIBO
 * */
public class ShareActivity extends Activity implements WbShareCallback, IUiListener {

    private static final String KEY_CHANNEL = "CHANNEL";
    private static final String KEY_ENTRY = "ENTITY";

    private WbShareHandler mWbShareHandler;
    private Tencent mTencent;

    private ShareEntity mEntity;
    private ShareChannel mChannel;

    private Share mShare;

    public static void start(Context context, ShareEntity entry, int channel) {
        Intent starter = new Intent(context, ShareActivity.class);
        starter.putExtra(KEY_ENTRY, entry);
        starter.putExtra(KEY_CHANNEL, channel);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WbSdk.install(this, new AuthInfo(this, Config.SINA_APP_ID, Config.REDIRECT_URL, Config.SCOPE));
        mWbShareHandler = new WbShareHandler(this);
        mWbShareHandler.registerApp();

        mTencent = QQAction.getTencent();

        mShare = Share.Registrar.getInstance().getShare();
        mEntity = getIntent().getParcelableExtra(KEY_ENTRY);
        int channel = getIntent().getIntExtra(KEY_CHANNEL, 0);
        if (mEntity == null || (channel != ShareChannel.QQ.getType() && channel != ShareChannel.QZONE.getType() && channel != ShareChannel.WEIBO.getType())) {
            if (mShare != null) {
                mShare.failure("分享失败");
            }
            finish();
        } else {
            mChannel = ShareChannel.from(channel);
            share();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if (mChannel == ShareChannel.WEIBO) {
            mWbShareHandler.doResultIntent(intent, this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == Constants.REQUEST_QQ_SHARE || requestCode == Constants.REQUEST_QZONE_SHARE){
            Tencent.handleResultData(data, this);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTencent != null) mTencent.releaseResource();
    }

    @Override
    public void onWbShareSuccess() {
        success();
    }

    @Override
    public void onWbShareCancel() {
        cancel();
    }

    @Override
    public void onWbShareFail() {
        fail();
    }

    @Override
    public void onComplete(Object o) {
        success();
    }

    @Override
    public void onError(UiError uiError) {
        fail();
    }

    @Override
    public void onCancel() {
        cancel();
    }

    private void success() {
        if (mShare != null) {
            mShare.success();
        }
        finish();
    }

    private void cancel() {
        if (mShare != null) {
            mShare.cancel();
        }
        finish();
    }

    private void fail() {
        if (mShare != null) {
            mShare.failure("分享失败");
        }
        finish();
    }

    private void share() {
        if (mChannel == ShareChannel.WEIBO) {
            WeiboMultiMessage message = new WeiboMultiMessage();
            TextObject text = new TextObject();
            text.text = mEntity.title + " " + mEntity.content + " " + mEntity.url;
            message.textObject = text;
            ImageObject image = new ImageObject();
            image.imagePath = mEntity.imagePath;
            message.imageObject = image;

            mWbShareHandler.shareMessage(message, true);
        } else {
            if (mTencent == null) {
                fail();
                return ;
            }
            if (mChannel == ShareChannel.QQ) {
                if(mEntity.isImageShare()){
                    shareToQQImage();
                }else {
                    shareToQQDefault();
                }
            } else {
                shareToQzone();
            }
        }
    }

    /**
     * 图文分享
     * SHARE_TO_QQ_TITLE 必填
     * SHARE_TO_QQ_TARGET_URL 必填
     * SHARE_TO_QQ_KEY_TYPE 必填
     *
     * */
    void shareToQQDefault(){
        Bundle params = new Bundle();
        params.putString(QQShare.SHARE_TO_QQ_TITLE, mEntity.title);
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY,  mEntity.content);
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  mEntity.url);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME,  "唯品金融");
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, mEntity.imageUrl);
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,QQShare.SHARE_TO_QQ_TYPE_DEFAULT);//图文分享
        mTencent.shareToQQ(this, params, this);
    }
    /**
     * 图片分享
     * SHARE_TO_QQ_IMAGE_LOCAL_URL 必填
     * SHARE_TO_QQ_KEY_TYPE 必填
     * */
    void shareToQQImage(){
        Bundle params = new Bundle();
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, mEntity.imagePath);
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "唯品金融");
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
        mTencent.shareToQQ(this, params, this);
    }

    /**
     * 分享到qq空间，目前只支持图文分享
     * SHARE_TO_QQ_TITLE 必填
     * SHARE_TO_QQ_TARGET_URL 必填
     * */
    void shareToQzone(){
        Bundle params = new Bundle();
        ArrayList<String> imageUrls = new ArrayList<String>(){{
            add(mEntity.imageUrl);
        }};
        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, mEntity.title);//必填
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY,  mEntity.content);
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL,  mEntity.url);//必填
        params.putString(QzoneShare.SHARE_TO_QQ_APP_NAME,  "唯品金融");
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        mTencent.shareToQzone(this, params, this);
    }

}
